package com.ship.dispatch.service.impl;

import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ship.common.core.domain.R;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.mapping.Label;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.dispatch.bean.PictureUrl;
import com.ship.dispatch.bean.SpYearPlanSchemeDetail;
import com.ship.dispatch.mapper.SpYearPlanSchemeDetailExMapper;
import com.ship.dispatch.service.SpYearPlanSchemeDetailExService;
import com.ship.dispatch.service.SpYearlyPlanService;
import com.ship.dispatch.vo.SpYearPlanDetailExportVo;
import com.ship.dispatch.vo.SpYearPlanSchemeDetailExportVo;
import com.ship.system.api.RemoteFileService;
import com.ship.system.api.domain.SysFile;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service("SpYearPlanSchemeDetailExService")
public class SpYearPlanSchemeDetailExServiceImpl extends ShipBaseServiceImpl<SpYearPlanSchemeDetailExMapper, SpYearPlanDetailExportVo> implements SpYearPlanSchemeDetailExService {

    @Autowired
    private SpYearPlanSchemeDetailExMapper spYearPlanSchemeDetailExMapper;

    @Override
    public List<SpYearPlanDetailExportVo> getYearPlanSchemeDetailList() {
        List<String> columnKeys = spYearPlanSchemeDetailExMapper.getTableColumnList();
        return spYearPlanSchemeDetailExMapper.getYearPlanSchemeDetailList(columnKeys);
    }

    @Autowired
    private SpYearlyPlanService spYearlyPlanService;

    @Autowired
    private RemoteFileService remoteFileService;

    @Value("${file_server.host}")
    private String fileServerHost;

    @Override
    public List<String> getTableColumnList() {
        return spYearPlanSchemeDetailExMapper.getTableColumnList();
    }

    @Override
    public String exportYearlyPlanDetailAndGante(SpYearPlanSchemeDetail spYearPlanSchemeDetail, String fileUrl,Integer size) {
//        Field[] fields = SpYearPlanDetailExportVo.class.getDeclaredFields();
//        List<SpYearPlanDetailExportVo> yearlyPlanList = getYearPlanSchemeDetailList();
//        for (SpYearPlanDetailExportVo planDetail : yearlyPlanList) {
//            BigDecimal singleGoodsQuantity = planDetail.getSingleGoodsQuantity();
//            String singleGoodsQuantityString = singleGoodsQuantity + " MMBtu";
//            planDetail.setSingleGoodsQuantityString(singleGoodsQuantityString);
//        }
//        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式化器
//        List<String> table = getTableColumnList();
//        List<String> idsStr = spYearPlanSchemeDetail.getIds();
//        List<Long> ids = idsStr.stream()
//                .map(Long::parseLong) // 将字符串转换为长整型数字
//                .collect(Collectors.toList());
//        List<SpYearPlanSchemeDetail> filteredList = new ArrayList<>();
//        if (ids.isEmpty()) {
//            filteredList = yearlyPlanList.stream()
//                    .filter(plan -> plan.getLastDetailId() != null) // 检查 lastDetailId 是否不为 null
//                    .collect(Collectors.toList());
//        }else {
//            filteredList = yearlyPlanList.stream()
//                    .filter(plan -> ids.contains(plan.getId()) && (plan.getLastDetailId() != null)) // 检查 id 是否存在于 ids 列表中 并且 lastDetailId 不为 null
//                    .collect(Collectors.toList());
//        }
//        try (Workbook workbook = new XSSFWorkbook()) {
//            // 创建一个工作表
//            Sheet sheet = workbook.createSheet("Filtered Data");
//
//            int rowNum = 0; // 开始时写入列名
//            Row headerRow = sheet.createRow(rowNum++);
//
//            for (int i = 0; i < fields.length; i++) {
//                fields[i].setAccessible(true);
//                String fieldName = fields[i].getName();
//                if (table.contains(fieldName)) {
//                    Cell cell = headerRow.createCell(i);
//                    cell.setCellValue(getFieldLabel(fields[i]));
//                } else {
//                    sheet.setColumnHidden(i, true);
//                }
//            }
//            for (SpYearPlanDetailExportVo plan : filteredList) {
//                Row row = sheet.createRow(rowNum++);
//                for (int i = 0; i < fields.length; i++) {
//                    fields[i].setAccessible(true);
//                    String fieldName = fields[i].getName();
//                    if (table.contains(fieldName)) {
//                        Object value = fields[i].get(plan);
//                        // 检查字段是否为Date类型，如果是则格式化为字符串
//                        if (value instanceof Date) {
//                            Cell cell = row.createCell(i);
//                            cell.setCellValue(dateFormatter.format((Date) value));
//                        } else if (fieldName.equals("state")) { // 特殊处理 state 列
//                            int stateValue = (int) value;
//                            String stateText;
//                            switch (stateValue) {
//                                case 1:
//                                    stateText = "草稿";
//                                    break;
//                                case 2:
//                                    stateText = "审批中";
//                                    break;
//                                case 3:
//                                    stateText = "已下发";
//                                    break;
//                                default:
//                                    stateText = "";
//                                    break;
//                            }
//                            row.createCell(i).setCellValue(stateText);
//                        }else if (fieldName.equals("tradeBody")) {
//                            int stateValue = (int) value;
//                            String stateText;
//                            switch (stateValue) {
//                                case 1:
//                                    stateText = "国贸";
//                                    break;
//                                case 2:
//                                    stateText = "新贸";
//                                    break;
//                                default:
//                                    stateText = "";
//                                    break;
//                            }
//                            row.createCell(i).setCellValue(stateText);
//                        }
//                        else if (fieldName.equals("tradeType")) {
//                            int stateValue = (int) value;
//                            String stateText;
//                            switch (stateValue) {
//                                case 1:
//                                    stateText = "长协";
//                                    break;
//                                case 2:
//                                    stateText = "中期";
//                                    break;
//                                case 3:
//                                    stateText = "现货";
//                                    break;
//                                default:
//                                    stateText = "";
//                                    break;
//                            }
//                            row.createCell(i).setCellValue(stateText);
//                        } else {
//                            row.createCell(i).setCellValue(value != null ? value.toString() : "");
//                        }
//                    }
//                }
//            }
//
//            List<Integer> columnsToBeDeleted = new ArrayList<>();
//
//            // 标记需要删除的列
//            for (int i = 0; i < fields.length; i++) {
//                if (!table.contains(fields[i].getName())) {
//                    columnsToBeDeleted.add(i); // 将不在表中的列的索引添加到列表中
//                } else {
//                    boolean isEmptyColumn = true;
//                    for (int j = 1; j < rowNum; j++) {
//                        Cell cell = sheet.getRow(j).getCell(i);
//                        if (cell != null && cell.getCellType() != CellType.BLANK) {
//                            isEmptyColumn = false;
//                            break;
//                        }
//                    }
//                    if (isEmptyColumn) {
//                        columnsToBeDeleted.add(i); // 将为空的列的索引添加到列表中
//                    }
//                }
//            }
//
//            // 删除标记的列
//            for (int i = columnsToBeDeleted.size() - 1; i >= 0; i--) {
//                int columnIndex = columnsToBeDeleted.get(i);
//                int lastColumn = sheet.getRow(0).getLastCellNum();
//                if (lastColumn > columnIndex) {
//                    for (int j = columnIndex; j < lastColumn - 1; j++) {
//                        for (int k = 0; k <= sheet.getLastRowNum(); k++) {
//                            Cell sourceCell = sheet.getRow(k).getCell(j + 1);
//                            Cell targetCell = sheet.getRow(k).getCell(j);
//                            if (targetCell == null) {
//                                targetCell = sheet.getRow(k).createCell(j);
//                            }
//                            if (sourceCell != null) {
//                                targetCell.setCellStyle(sourceCell.getCellStyle());
//                                targetCell.setCellType(sourceCell.getCellType());
//                                switch (sourceCell.getCellType()) {
//                                    case STRING:
//                                        targetCell.setCellValue(sourceCell.getStringCellValue());
//                                        break;
//                                    case NUMERIC:
//                                        targetCell.setCellValue(sourceCell.getNumericCellValue());
//                                        break;
//                                    case BOOLEAN:
//                                        targetCell.setCellValue(sourceCell.getBooleanCellValue());
//                                        break;
//                                    // Handle other cell types as needed
//                                }
//                            }
//                        }
//                    }
//                    for (int k = 0; k <= sheet.getLastRowNum(); k++) {
//                        sheet.getRow(k).removeCell(sheet.getRow(k).getCell(lastColumn - 1));
//                    }
//                }
//            }
//
//            // 取消所有隐藏列
//            for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
//                sheet.setColumnHidden(i, false);
//            }
        String CsoldPart = "http://10.75.34.224:9300/statics";
        String ZsoldPart = "http://10.75.33.149:9300/statics";
        String CsPart = "https://lngshippingcs.cgp.cnooc/api/file/statics";
        String ZsPart = "https://lngshipping.cgp.cnooc/api/file/statics";

        try {

            String filename;
            // 获取当前日期
            LocalDateTime currentDate = LocalDateTime.now();
            // 格式化日期为指定格式（例如：年月日）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH.mm");
            String formattedDate = currentDate.format(formatter);

            String SellerId = spYearPlanSchemeDetail.getSellerId();
            int currentYear = Year.now().getValue()+1;
            int idsVal = size;
            String zipFilename;
            if (SellerId.isEmpty()) {
                zipFilename = currentYear+"年度计划-"+idsVal+"船货"+formattedDate+".zip";
                filename = currentYear+"年度计划-"+idsVal+"船货"+formattedDate+".xlsx";
            }else {
                String searchItem = spYearPlanSchemeDetailExMapper.getSeller(SellerId);
                zipFilename = currentYear+"年度计划-"+ searchItem + "-" +idsVal+"船货"+formattedDate+".zip";
                filename = currentYear+"年度计划-"+ searchItem + "-" +idsVal+"船货"+formattedDate+".xlsx";
            }

            // 将工作簿保存到文件
            String newUrl = "";
            if (fileUrl.contains("https://lngshipping.cgp.cnooc")) {
                newUrl = fileUrl.replace(ZsPart, ZsoldPart);
            }else if (fileUrl.contains("https://lngshippingcs.cgp.cnooc")){
                newUrl = fileUrl.replace(CsPart, CsoldPart);
            }else {
                newUrl = fileUrl;
            }
            try {
                URL url = new URL(newUrl);
                URLConnection conn = url.openConnection();
                InputStream inputStream = conn.getInputStream();

                try (FileOutputStream outputStream = new FileOutputStream(filename)) {
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                }
            }catch (IOException e) {
                // 处理工作簿创建异常
                e.printStackTrace();
            }

            // 从 SpYearPlanSchemeDetailExportVo 中获取图片链接
            PictureUrl pictureUrl = spYearPlanSchemeDetail.getPictureUrl();
            String shipUrl = pictureUrl.getShipUrl();
//            String sellerUrl = pictureUrl.getSellerUrl();
            String goodsUrl = pictureUrl.getGoodsUrl();

            // 下载图片并保存到本地
            spYearlyPlanService.saveBase64Image(shipUrl, "船舶排布.jpg");
//            spYearlyPlanService.saveBase64Image(sellerUrl, "资源方排布.jpg");
            spYearlyPlanService.saveBase64Image(goodsUrl, "货源排布.jpg");

            // 创建一个压缩包并添加这些图片文件
//            createZipFile(zipFilename, filename,"船舶排布.jpg", "资源方排布.jpg", "货源排布.jpg");
            createZipFile(zipFilename, filename,"船舶排布.jpg", "货源排布.jpg");

            //文件地址
            File file = new File(zipFilename);
//            //声明参数集合
//            HashMap<String, Object> paramMap = new HashMap<>();
//            //文件
//            paramMap.put("file", file);
//            //输出
//            paramMap.put("output","json");
//            //自定义路径
//            paramMap.put("path","image");
//            //场景
//            paramMap.put("scene","image");
//
//            paramMap.put("filename",zipFilename);

            //上传
//            String result= HttpUtil.post("http://106.12.241.246:8083/group1/upload", paramMap);
            FileInputStream input = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "application/octet-stream", input);
            R<SysFile> result1 = remoteFileService.upload(multipartFile);
            if(result1.getCode() != R.SUCCESS){
                throw new BusException("文件服务器连接失败", BaseResultCode.GENERAL_ERROR);
            }
            String url = result1.getData().getHttpsUrl();
            // 上传完成后删除压缩包
            File zipFile = new File(zipFilename);
            File excelFile = new File(filename);
            File picture1 = new File("船舶排布.jpg");
            File picture2 = new File("货源排布.jpg");
//            File picture3 = new File("资源方排布.jpg");
            if (zipFile.exists()) {
                if (zipFile.delete()) {
                    excelFile.delete();
                    picture1.delete();
                    picture2.delete();
//                    picture3.delete();
                }
            }
            // 使用Gson解析JSON
//            Gson gson = new Gson();
//            JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
//            // 获取"url"字段的值
//            String url = jsonObject.get("url").getAsString();
            return url;
        } catch (IOException e) {
            // 处理工作簿创建异常
            e.printStackTrace();
            return"创建 Excel 工作簿失败";
        }
    }

    @Override
    public String exportYearlyPlanDetail(SpYearPlanSchemeDetailExportVo spYearPlanSchemeDetailExportVo) {
        Field[] fields = SpYearPlanDetailExportVo.class.getDeclaredFields();
        List<SpYearPlanDetailExportVo> yearlyPlanList = getYearPlanSchemeDetailList();
        for (SpYearPlanDetailExportVo planDetail : yearlyPlanList) {
            BigDecimal singleGoodsQuantity = planDetail.getSingleGoodsQuantity();
            String singleGoodsQuantityString = singleGoodsQuantity + " MMBtu";
            planDetail.setSingleGoodsQuantityString(singleGoodsQuantityString);
        }
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式化器
        List<String> table = getTableColumnList();
        List<String> idsStr = spYearPlanSchemeDetailExportVo.getIds();
        List<Long> ids = idsStr.stream()
                .map(Long::parseLong) // 将字符串转换为长整型数字
                .collect(Collectors.toList());
        List<SpYearPlanDetailExportVo> filteredList;
        if (ids.isEmpty()) {
            filteredList = yearlyPlanList.stream()
                    .filter(plan -> plan.getLastDetailId() != null) // 检查 lastDetailId 是否不为 null
                    .collect(Collectors.toList());
        }else {
            filteredList = yearlyPlanList.stream()
                    .filter(plan -> ids.contains(plan.getId()) && (plan.getLastDetailId() != null)) // 检查 id 是否存在于 ids 列表中 并且 lastDetailId 不为 null
                    .collect(Collectors.toList());
        }
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建一个工作表
            Sheet sheet = workbook.createSheet("Filtered Data");

            int rowNum = 0; // 开始时写入列名
            Row headerRow = sheet.createRow(rowNum++);

            for (int i = 0; i < fields.length; i++) {
                fields[i].setAccessible(true);
                String fieldName = fields[i].getName();
                if (table.contains(fieldName)) {
                    Cell cell = headerRow.createCell(i);
                    cell.setCellValue(getFieldLabel(fields[i]));
                } else {
                    sheet.setColumnHidden(i, true);
                }
            }
            for (SpYearPlanDetailExportVo plan : filteredList) {
                Row row = sheet.createRow(rowNum++);
                for (int i = 0; i < fields.length; i++) {
                    fields[i].setAccessible(true);
                    String fieldName = fields[i].getName();
                    if (table.contains(fieldName)) {
                        Object value = fields[i].get(plan);
                        // 检查字段是否为Date类型，如果是则格式化为字符串
                        if (value instanceof Date) {
                            Cell cell = row.createCell(i);
                            cell.setCellValue(dateFormatter.format((Date) value));
                        } else if (fieldName.equals("state")) { // 特殊处理 state 列
                            int stateValue = (int) value;
                            String stateText;
                            switch (stateValue) {
                                case 1:
                                    stateText = "草稿";
                                    break;
                                case 2:
                                    stateText = "审批中";
                                    break;
                                case 3:
                                    stateText = "已下发";
                                    break;
                                default:
                                    stateText = "";
                                    break;
                            }
                            row.createCell(i).setCellValue(stateText);
                        }else if (fieldName.equals("tradeBody")) {
                            int stateValue = (int) value;
                            String stateText;
                            switch (stateValue) {
                                case 1:
                                    stateText = "国贸";
                                    break;
                                case 2:
                                    stateText = "新贸";
                                    break;
                                default:
                                    stateText = "";
                                    break;
                            }
                            row.createCell(i).setCellValue(stateText);
                        }
                        else if (fieldName.equals("tradeType")) {
                            int stateValue = (int) value;
                            String stateText;
                            switch (stateValue) {
                                case 1:
                                    stateText = "长协";
                                    break;
                                case 2:
                                    stateText = "中期";
                                    break;
                                case 3:
                                    stateText = "现货";
                                    break;
                                default:
                                    stateText = "";
                                    break;
                            }
                            row.createCell(i).setCellValue(stateText);
                        }
                        else {
                            row.createCell(i).setCellValue(value != null ? value.toString() : "");
                        }
                    }
                }
            }

            List<Integer> columnsToBeDeleted = new ArrayList<>();

            // 标记需要删除的列
            for (int i = 0; i < fields.length; i++) {
                if (!table.contains(fields[i].getName())) {
                    columnsToBeDeleted.add(i); // 将不在表中的列的索引添加到列表中
                } else {
                    boolean isEmptyColumn = true;
                    for (int j = 1; j < rowNum; j++) {
                        Cell cell = sheet.getRow(j).getCell(i);
                        if (cell != null && cell.getCellType() != CellType.BLANK) {
                            isEmptyColumn = false;
                            break;
                        }
                    }
                    if (isEmptyColumn) {
                        columnsToBeDeleted.add(i); // 将为空的列的索引添加到列表中
                    }
                }
            }

            // 删除标记的列
            for (int i = columnsToBeDeleted.size() - 1; i >= 0; i--) {
                int columnIndex = columnsToBeDeleted.get(i);
                int lastColumn = sheet.getRow(0).getLastCellNum();
                if (lastColumn > columnIndex) {
                    for (int j = columnIndex; j < lastColumn - 1; j++) {
                        for (int k = 0; k <= sheet.getLastRowNum(); k++) {
                            Cell sourceCell = sheet.getRow(k).getCell(j + 1);
                            Cell targetCell = sheet.getRow(k).getCell(j);
                            if (targetCell == null) {
                                targetCell = sheet.getRow(k).createCell(j);
                            }
                            if (sourceCell != null) {
                                targetCell.setCellStyle(sourceCell.getCellStyle());
                                targetCell.setCellType(sourceCell.getCellType());
                                switch (sourceCell.getCellType()) {
                                    case STRING:
                                        targetCell.setCellValue(sourceCell.getStringCellValue());
                                        break;
                                    case NUMERIC:
                                        targetCell.setCellValue(sourceCell.getNumericCellValue());
                                        break;
                                    case BOOLEAN:
                                        targetCell.setCellValue(sourceCell.getBooleanCellValue());
                                        break;
                                    // Handle other cell types as needed
                                }
                            }
                        }
                    }
                    for (int k = 0; k <= sheet.getLastRowNum(); k++) {
                        sheet.getRow(k).removeCell(sheet.getRow(k).getCell(lastColumn - 1));
                    }
                }
            }

            // 取消所有隐藏列
            for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
                sheet.setColumnHidden(i, false);
            }

            // 将工作簿保存到文件
            try (FileOutputStream outputStream = new FileOutputStream("年度计划详情表.xlsx")) {
                workbook.write(outputStream);
            } catch (IOException e) {
                // 处理文件写入异常
                e.printStackTrace();
                return "导出 Excel 文件失败";
            }
            //文件地址
            File file = new File("年度计划详情表.xlsx");
//            //声明参数集合
//            HashMap<String, Object> paramMap = new HashMap<>();
//            //文件
//            paramMap.put("file", file);
//            //输出
//            paramMap.put("output","json");
//            //自定义路径
//            paramMap.put("path","image");
//            //场景
//            paramMap.put("scene","image");
            // 获取当前日期
            LocalDateTime currentDate = LocalDateTime.now();
            // 格式化日期为指定格式（例如：年月日）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH.mm");
            String formattedDate = currentDate.format(formatter);

            String searchItem = spYearPlanSchemeDetailExportVo.getSearchItem();
            int currentYear = Year.now().getValue()+1;
            int idsVal = filteredList.size();
            String filename;
            if (searchItem.isEmpty()) {
                filename = currentYear+"年度计划-"+idsVal+"船货"+formattedDate+".xlsx";
            }else {
                filename = currentYear+"年度计划-"+ searchItem + "-" +idsVal+"船货"+formattedDate+".xlsx";
            }
//            paramMap.put("filename",filename);
            FileInputStream input = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "application/octet-stream", input);
            R<SysFile> result1 = remoteFileService.upload(multipartFile);
            if(result1.getCode() != R.SUCCESS){
                throw new BusException("文件服务器连接失败", BaseResultCode.GENERAL_ERROR);
            }
            String url = result1.getData().getHttpsUrl();

            //上传
//            String result= HttpUtil.post(fileServerHost+"/group1/upload", paramMap);

            // 上传完成后删除本地文件
            File excelFile = new File(filename);
            if (excelFile.exists()) {
                if (excelFile.delete()) {
                }
            }

            // 使用Gson解析JSON
//            Gson gson = new Gson();
//            JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
            // 获取"url"字段的值
//            String url = jsonObject.get("url").getAsString();
            if (file.exists()) {
                if (file.delete()) {
                }
            }
            return url;
        } catch (IOException e) {
            // 处理工作簿创建异常
            e.printStackTrace();
            return "创建 Excel 工作簿失败";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public void createZipFile(String zipFileName, String... filesToAdd) {
        try {
            FileOutputStream fos = new FileOutputStream(zipFileName);
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (String fileToAdd : filesToAdd) {
                addToZipFile(fileToAdd, zos);
            }

            zos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            // 处理文件未找到异常
        } catch (IOException e) {
            e.printStackTrace();
            // 处理其他 IO 异常
        }
    }

    private void addToZipFile(String fileName, ZipOutputStream zos) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);

        ZipEntry zipEntry = new ZipEntry(fileName);
        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }

        zos.closeEntry();
        fis.close();
    }

    private String getFieldLabel(Field field) {
        Label labelAnnotation = field.getAnnotation(Label.class);
        if (labelAnnotation != null) {
            String[] labelValues = labelAnnotation.value();
            if (labelValues.length > 0) {
                return labelValues[0]; // 返回数组的第一个元素作为Label值
            }
        }
        return field.getName(); // 如果注解不存在或者数组为空，返回字段名作为默认值
    }
}
